package cn.com.surker.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author lucky
 * @create 2021年02月22 11:44
 * @desc
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private ConfigInterceptor handlerInterceptor;
    @Autowired
    private ErpLogInterceptor appletLogInterceptor;

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
			.allowCredentials(true)
			.allowedOrigins("*")
			.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
			.maxAge(3600)
			.allowedHeaders("*");
	}

    /**
     * 拦截所有请求 放行后台页面的请求
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/.well-known/**")//https认证
                .excludePathPatterns("/5byalH00NH.txt")//微信小程序认证
                .excludePathPatterns("/MP_verify_V5a7H9AJlB1olkiX.txt")//微信公众号认证
                .excludePathPatterns("/WW_verify_x3z5YfF8BrYkKRJ5.txt")//企业微信认证
                .excludePathPatterns("/wechat/account/**")//微信公众号打卡签到
                .excludePathPatterns("/wechat/order/**")//微信订单系统
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/console/login")//电脑版登录
                .excludePathPatterns("/error/**")
                .excludePathPatterns("/weigh/api/**")//抽检接口白名单
                .excludePathPatterns("/cloud/production/api/**")//云砼接入接口白名单
                .excludePathPatterns("/weigh/message/**")//过磅消息推送
                .excludePathPatterns("/production/api/**")//过磅消息推送
                .excludePathPatterns("/cloud/login")//手机端登录
                .excludePathPatterns("/wechat/login")//未小程序登录
                .excludePathPatterns("/applet/login")//未小程序登录
                .excludePathPatterns("/applet/printed/text")//通用印刷体识别
                .excludePathPatterns("/cloud/gps/waring")//GPS预警页
                .excludePathPatterns("/user/cloud/login")
                .excludePathPatterns("/expRecord/load/image")//技术研发图片接口
                .excludePathPatterns("/cloud/morning/**")//早报接口
                .excludePathPatterns("/cloud/report/plan/task/list")//调度生产计划推送
                .excludePathPatterns("/report/record/data")
				.excludePathPatterns("/website/news/list")
				.excludePathPatterns("/console/material/stock-line")
                .excludePathPatterns("/message/material/**")
                .excludePathPatterns("/material/stock/line")
                .excludePathPatterns("/contract/send/message")
                .excludePathPatterns("/material/stock/qrcode")
			.excludePathPatterns("/material/stock/qrcode")
			.excludePathPatterns("/console/auth/code")
			.excludePathPatterns("/common/api/**");
        registry.addInterceptor(appletLogInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/error/**");
    }

    /**
     * 添加静态资源放行
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        WebMvcConfigurer.super.addResourceHandlers(registry);

    }

}
